package com.ojoin.trade.t0loan_core.web.controller;

import com.ojoin.trade.common.Constants;
import com.ojoin.trade.common.web.controller.SimpleBaseController;
import com.ojoin.trade.common.web.domain.Result;
import com.ojoin.trade.common_core.web.domain.TbBank;
import com.ojoin.trade.common_core.web.domain.TbBranch;
import com.ojoin.trade.common_core.web.service.BankBaseService;
import com.ojoin.trade.common_core.web.service.BranchBaseService;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.net.URLDecoder;
import java.util.List;

/**
 * Created with IntelliJ IDEA. User: Administrator Date: 15-8-20 Time: 下午2:34 To
 * change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/bank")
public class BankController  extends SimpleBaseController {

	private org.slf4j.Logger log = LoggerFactory
			.getLogger(BankController.class);

	@Autowired
	private BankBaseService service;
	
	@Autowired
	private BranchBaseService branchservice;

	@ResponseBody
	@RequestMapping(value = "/queryBank", method = RequestMethod.GET)
	public Result queryBank() {
		try {
			List<TbBank> list = service.queryAll();
			return new Result(Result.Status.OK, list);
		} catch (Exception e) {
			log.error("查询银行信息失败", e);
			return new Result(Result.Status.OK, "查询银行信息失败");
		}
	}

	@ResponseBody
	@RequestMapping(value = "/queryBankBranch", method = RequestMethod.GET)
	public Result queryBankBranch(String name, String area_id) {

		if (StringUtils.isEmpty(name)) {
			return new Result(Result.Status.ERROR, "请先选择所属银行");

		}
		if (StringUtils.isEmpty(area_id)) {
			return new Result(Result.Status.ERROR, "请先选择市");
		}
		try {
            name= URLDecoder.decode(name,Constants.ENCODING);
			List<TbBranch> list = branchservice.queryBankBranch(area_id, name);
			return new Result(Result.Status.OK, list);
		} catch (Exception e) {
			log.error("查询银行分支号失败", e);
			return new Result(Result.Status.ERROR, "查询银行分支号失败");
		}

	}

}
